package InterFaceContact;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

public class ArquivosConfiguracao {

	static String nameSimulation = new String();
	static String path = "simulations/", N = "network", S = "simulation", T = "trafic", P = "position";
	
	//Inserir nome da simulacao, passar para ca o nome da simulacao
	
	public ArquivosConfiguracao () throws IOException{
		readInfo();
	}
	
	public static String getNameSimulation() {
		return nameSimulation;
	}

	public static void setNameSimulation(String nameSimulation) {
		ArquivosConfiguracao.nameSimulation = nameSimulation;
	}

	public static void readInfo () throws IOException{
		System.out.println(ArquivosConfiguracao.nameSimulation+"\n");
		
		File path = new File(ArquivosConfiguracao.path+ArquivosConfiguracao.nameSimulation+"/");
		if (path.exists()) path.delete();
		path.mkdirs();
		
		readNetwork();
		readSimulation();
		readTrafic();
		readPositions();
	}
	
	public static void readCellsPosition(HashMap<String, Object> cells) {
		

	}
	
	public static void readNetwork () throws IOException{
		File data = new File(ArquivosConfiguracao.path+ArquivosConfiguracao.nameSimulation+"/"+ArquivosConfiguracao.N);
		if (data.exists())data.delete();
		data.createNewFile();
		
		
		FileWriter fw = new FileWriter(new File(ArquivosConfiguracao.path +ArquivosConfiguracao.nameSimulation+"/"+ ArquivosConfiguracao.N));
		BufferedWriter bw = new BufferedWriter(fw);
		Iterator<String> in = Network.nos.keySet().iterator();
		int i;
		
		bw.write("node:\n");
		for(i = 0; i < Network.nos.keySet().size(); i++){
			bw.write(i+1+"\n");
		};
		bw.write("links:\n");
		
		while(in.hasNext()){
			String aux = in.next();
			Iterator<String> in2 = Network.nos.get(aux).keySet().iterator();
			
			while(in2.hasNext()){
				String aux2 = in2.next();
				
				bw.write(aux+";"+aux2+";"+Network.nos.get(aux).get(aux2).getValor()
						+";"+Network.nos.get(aux).get(aux2).getNumSlots()
						+";"+Network.nos.get(aux).get(aux2).getFre()
						+";"+Network.nos.get(aux).get(aux2).getDist()
						+"\n"
				);	
			}
		}
		bw.close();
		fw.close();
	}
	
	public static void readSimulation() throws IOException{
		File data = new File(ArquivosConfiguracao.path + ArquivosConfiguracao.nameSimulation+"/"+ArquivosConfiguracao.S);
		if (data.exists())data.delete();
		data.createNewFile();
		
		FileWriter fw = new FileWriter(new File(ArquivosConfiguracao.path + ArquivosConfiguracao.nameSimulation+"/"+ArquivosConfiguracao.S));
		BufferedWriter bw = new BufferedWriter(fw);
		
		bw.write("//teste simulationParser\n");
		bw.write("requests:"+Simulation.getNumRequest()+"\n");
		bw.write("rsa:"+Simulation.getRsaType()+";"+Simulation.getRsaRouting()+";"+Simulation.getRsaPolicy()+"\n");
		bw.write("loadPoints:"+Simulation.getLoadPoints()+"\n");
		bw.write("replications:"+Simulation.getReplic()+"\n");
		bw.write("significance:"+Simulation.getSig()+"\n");
		bw.close();
		fw.close();
		
	}

	public static void readTrafic() throws IOException{
		File data = new File(ArquivosConfiguracao.path+ArquivosConfiguracao.nameSimulation+"/"+ArquivosConfiguracao.T);
		if (data.exists())data.delete();
		data.createNewFile();
		
		FileWriter fw = new FileWriter(new File(ArquivosConfiguracao.path+ArquivosConfiguracao.nameSimulation+"/"+ArquivosConfiguracao.T));
		BufferedWriter bw = new BufferedWriter(fw);
		Iterator<String> in = Trafic.config.keySet().iterator();
		
		while(in.hasNext()){
			String aux = in.next();
			Iterator<String> in2 = Trafic.config.get(aux).keySet().iterator();
			
			while(in2.hasNext()){
				String aux2 = in2.next();
				
				if(aux != aux2){
					bw.write(aux+"->"+aux2+"\n");
					for (Request request : Trafic.config.get(aux).get(aux2)) {
						
					bw.write("\t"+request.getSize()
							+";"+request.getArrive()
							+";"+request.getAtend()
							+";"+request.getIncr()	+"\n");	
					}
				}
			}
		}
		bw.close();
		fw.close();
		
	}

	public static void readPositions() throws IOException{
		File data = new File(ArquivosConfiguracao.path+ArquivosConfiguracao.nameSimulation+"/"+ArquivosConfiguracao.P);
		if (data.exists())data.delete();
		data.createNewFile();
		
		FileWriter fw = new FileWriter(new File(ArquivosConfiguracao.path+ArquivosConfiguracao.nameSimulation+"/"+ArquivosConfiguracao.P));
		BufferedWriter bw = new BufferedWriter(fw);
		Iterator<String> in = Position.getPos().keySet().iterator();
		String aux;
		
		bw.write(nameSimulation+"\n");
		bw.write("Positions:\n");
		while(in.hasNext()){
			aux = in.next();
			
			bw.write(aux+" "+Position.getPos().get(aux)[0]+" "+Position.getPos().get(aux)[1]+"\n");
		
		}
		
		bw.write("Lig:\n");
		in = Position.getPos().keySet().iterator();
		while(in.hasNext()){
			aux = in.next();
			
			for(int i = 0; i<Position.getLig().get(aux).size(); i++){
				bw.write(aux+" "+Position.getLig().get(aux).get(i)+"\n");
			}
		}
		
		bw.close();
		fw.close();
	}
}
